<?php

namespace Pub\Controller;

use Common\Controller\BaseController;

/**
 *  个人信息
 *  @author zangyilei
 */

class AccountController extends BaseController {
    
    public function _initialize(){
        parent::_initialize();
        if (empty($this->iUserId)){
            $this->error("请登录", -103);
        }
    }

    /**
     *  账户修改
     */
    public function update(){
        
        $objectUser = M("User");
        
        if (!$objectUser->create()) {
            $this->error($objectUser->getError());
        }
        
        unset($objectUser->photo);
        $_FILES['photo'] && $objectUser->photo = $this->uploadfile('photo','Account',1024*1024*2);
        $objectUser->user_id = $this->iUserId;
        $objectUser->birthday = $objectUser->birthday.'-00';
        $objectUser->update_time = format_time();
        
        $res = $objectUser->save();
        if($res){
            
            $this->success('保存成功',1);
        }else {
            $this->error('保存失败');
        }
    }
    
    /**
     *  账户详情
     */
    public function detail(){
        $userInfo = M("User")
            ->field("user_id,user_name,real_name,phone,sex,date_format(birthday,'%Y-%m-%d') AS birthday,photo,position,create_time")
            ->where(array('qy_user.user_id'=>$this->iUserId))
            ->find();
        $userInfo['photo'] && $userInfo['photo'] = C('SITE_URL').$userInfo['photo'];
            
        $companyInfo = $this->getCompanyInfo();
        $userInfo['role_id']    = '';
        $userInfo['cate_name']  = '';
        if ($companyInfo){
            $userInfo['role_id']    = $companyInfo['role_id'];
            $userInfo['cate_name']  = $companyInfo['cate_name'];
            if ($companyInfo['role_id'] == 1){
                $userInfo['position']   = $userInfo['position']?$userInfo['position']:'总经理';
                $userInfo['cate_name']  = '运营，客服，美工，营销';
            }else if($companyInfo['role_id'] == 2){
                $userInfo['cate_name']  = '运营，客服，美工，营销';
            }
        }
        if (empty($userInfo)){
            $this->error('无法查到该用户信息');
        }
        
        $this->returnList($userInfo);
    }

    /**
     *  修改密码
     *
     */
    Public function changePwd(){
        $this->success('修改失败');
        $strPassword       = $this->getParam('password','','string',true,"原密码不能为空");
        $strNewpassword    = $this->getParam('newpassword','','string',true,"新密码不能为空");
        $strRepassword     = $this->getParam('repassword','','string',true,"确认密码不能为空");
        $User              = D('User');
    
        $rules = array(
            array('newpassword', '/^[\S]{6,12}$/', '密码长度要求6-12位，不限制类型', 1),
            array('repassword','newpassword','两次密码输入不一致',0,'confirm')
        );
         
        if(!$User->validate($rules)->create()) {
            $this->error($User->getError());
        }
         
        $password = M("User")->where(array('user_id'=>$this->iUserId))->getField('password');
        if ($password != md5($strPassword)){
            $this->error('原密码不正确');
        }
         
        $User->user_id       = $this->iUserId;
        $User->password      = md5($strNewpassword);
        $res                 = $User->save();
         
        if($res){
            $this->success('修改成功',1);
        }else {
            $this->success('修改失败');
        }
    }
    
    /**
     *  获取企业信息
     */
    protected function getCompanyInfo() {
        $where['qy_user_company_relat.status']  = array('IN','0,1');
        $where['qy_user_company_relat.user_id'] = $this->iUserId;
        $companyInfo = M("UserCompanyRelat")
                ->join('LEFT JOIN qy_category_all d on qy_user_company_relat.cate_id = d.cate_id ')
                ->field("qy_user_company_relat.role_id,d.cate_name")
                ->where($where)
                ->find();
        
        return $companyInfo;
    }
}